/**
 * Created by Administrator on 2019/3/21 0021.
 */
function initStationPost() {
    var longitude= $("#collectionlongitude").val();
    var latitude=  $("#collectionlatitude").val();
    //初始化离我我近的站信息
    var link = location.href;
    var formCollectionLately = new FormData();
    formCollectionLately.append("url",link);
    formCollectionLately.append("longitude",longitude);
    formCollectionLately.append("latitude",latitude);
    ajaxPostdata('collection/lately',formCollectionLately, collectionLately);

    //初始化使用频率最高站信息
    collectionBestHigth();
    //初始化我的收藏信息
    collectionMessage();
}
    function collectionLately(data) {
    //返回的是map信息
        console.log()
    $("#closest").empty();
    if(data.length == 0 || data == null){
        $("#closest").append("<h4 style='font-size: 34px;text-align: center;color: red'>附近暂无充电站服务</h4>");
    }else {
        for (var i = 0; i < data.length; i++) {
            var collection = data[i];
            //是否在线
            var  onlineStatus = "";
            if(collection.onlineStatus=="true"){
                onlineStatus = "在线";
            }else {
                onlineStatus = "离线";
            }
            var imgs ="";
            if(collection.Enabled=="1"){
                imgs ="<img style='margin: 0px 0px 0px 11px;' src='./images/wx_collectionOk.png' onclick='mpCollectionStationMassage(\""+collection.chargingstationid+"\")'/>";
            }else {
                imgs ="<img style='margin: 0px 0px 0px 11px;' src='./images/wx_map_collction.png' onclick='mpCollectionStationMassage(\""+collection.chargingstationid+"\")'/>";
            }
            var htmlClosestContent = "<div style='width: 100%;'>" +
                "<div style='width: 81%; float: left;border-top: 1px solid #F2F2F2'>" +
                "   <div style='width:100%;'>" +
                "   <div style='width: 28%;height:50px;float: left;font-size: 28px;color: black;text-align: left'>"+collection.chargingstationshortname+"</div>" +
                "   <div style='width: 72%;height:50px;float: left;'>" +
                "   <div style='text-align: left;margin-top: 5px;border: 1px solid rgba(25, 130, 226, 1);width: 45px;font-size: 20px;color: rgba(25, 130, 226, 1)'>"+onlineStatus+"</div>" +
                "   </div>" +
                "   </div>" +
                "   <div style='width:100%; height: 60px'>" +
                "   <div style='width: 15%;float: left'>" +
                "   <div style='margin-left:8px;text-align: left; font-size: 22px;color:rgba(153, 153, 153, 1)'>微信支付</div>" +
                "   </div>" +
                "   <div style='width: 85%;float: left'>" +
                "   <div style='text-align: left; font-size: 22px;color:rgba(153, 153, 153, 1)'>科佳运营</div></div>" +
                "   </div>" +
                "   <div style='width:100%;background-color: #316AC5'>" +
                "   <div style='width: 6%;float: left'><div style='margin-left: 5px;'><img src='./images/wx_map_fast.png'/></div></div>" +
                "   <div style='width: 6%;float: left'><div ><img src='./images/wx_map_sowle.png'/></div></div>" +
                "   <div style='width: 88%;float: left'><div><span style='font-size: 22px;color: rgba(28, 189, 111, 1)'>可用 &nbsp;"+collection.enableCount+"&nbsp;</span>/<span style='font-size: 22px;'>&nbsp;总计&nbsp;"+collection.allnumbers+"</span></div></div>" +
                "</div>" +
                "</div>" +
                "<div style='width: 19%; float: left;border-top: 1px solid #F2F2F2'>" +
                "   <div style='width:100%;height: 40px;text-align: center'></div>" +
                "   <div style='width:100%;height: 40px;position: relative;'>" +
                "   <div style='height: 40px;width: 40px;background-color: rgba(25, 130, 226, 1);position: absolute;top:50%;left: 50%;margin-top: -19px;margin-left: -17px;-webkit-border-radius: 20px;'>" +
                "   <img style='margin: 0px 0px 0px 11px;' onclick='openWeiXinInMap("+collection.longitude+","+collection.latitude+",\""+collection.chargingstationname+"\",\""+collection.chargingstationaddress+"\")'  src='./images/wx_map_point.png'/>" +
                "    </div>" +
                "     </div>" +
                "    <div style='width:100%;height: 40px;text-align: center'>"+collection.distanceM+"M</div>" +
                "</div>" +
                " </div>" +
                " <div style='width: 100%;'>" +
                "     <div style='width: 81%;float: left;border-top: 1px solid #F2F2F2;border-bottom: 1px solid #F2F2F2 '>" +
                "    <div style='width:100%;height: 40px;'>" +
                "   <div style='margin-left: 5px;text-align: left'><img style='height: 16px;width: 15px;' src='./images/wx_map_adress.png'/>"+collection.chargingstationaddress+"</div>" +
                "</div>" +
                "<div style='width:100%;height: 40px;'>" +
                "    <div style='margin-left: 5px;text-align: left'>电价：&nbsp;"+collection.electricityPrice+"元/度&nbsp;&nbsp;服务费：&nbsp;"+collection.serviceCharge+"元/度</div>" +
                "</div>" +
                "<div style='width:100%;height: 40px;'>" +
                "   <div style='margin-left: 5px;text-align: left'>营业时间：&nbsp;00:00-24:00&nbsp;&nbsp;联系方式：&nbsp;010-61945252</div>" +
                "</div>" +
                "</div>" +
                "<div style='width: 19%;float: left;border-top: 1px solid #F2F2F2;border-bottom: 1px solid #F2F2F2 ;height: 120px;'>" +
                "<div style='width:100%;height: 40px;position: relative;'>" +
                "<div id = '"+collection.chargingstationid+"'style='height: 40px;width: 40px;position: absolute;top:50%;left: 50%;margin-top: -19px;margin-left: -26px;'>" +
                imgs+
                "</div>" +
                " </div>" +
                " <div style='width:100%;height: 40px;text-align: center;color: rgba(153, 153, 153, 1)'>收藏</div>" +
                "</div>" +
                "</div>";
            $("#closest").append(htmlClosestContent);
        }
    }





    }
    function collectionBestHigth() {

    }
    function collectionMessage() {

    }

function ajaxPostdata(url,formData,callback) {
    $.ajax({
        url: url,
        type: "POST",
        data: formData,
        timeout: 200000, //通过timeout属性，设置超时时间
        contentType: false,
        processData: false,
        success: function (data) {
            if (typeof callback === 'function') {
                callback(data);
            }
        },
        error: function () {
        }
    });
};

/**
 * 点击收藏事件
 * @param chargingstationid
 */
function mpCollectionStationMassage(chargingstationid) {
    console.log("chargingstationid="+chargingstationid);
    var openid = "testUserOpenid";
    var imglist=$("#"+chargingstationid).children($("img"));
    //1收藏  0是取消收藏
    for(var i=0;i<imglist.length;i++){
     var ingStr = JSON.stringify(imglist[i].src);
       if (ingStr.indexOf("wx_map_collction.png") != -1 ){
          var form = new FormData();
           form.append("type","1");
           ajaxPostdata('mpCollectionStationMassage/'+chargingstationid+'/'+openid,form, changeImgColor);

       }else {
           var form = new FormData();
           form.append("type","0");
           ajaxPostdata('mpCollectionStationMassage/'+chargingstationid+'/'+openid,form, changeImgColor);

       }

    }

};
/**
 * 修改收藏图片的颜色
 * @param data
 */
function changeImgColor(data) {
    var  chargingstationid = data.chargingStationID;
    if(data.message == "success1"){
        $("#"+chargingstationid).empty();
        var img = "<img style='margin: 0px 0px 0px 11px;' src='./images/wx_collectionOk.png' onclick='mpCollectionStationMassage(\""+chargingstationid+"\")'/>";
        $("#"+chargingstationid).append(img);
    }else if(data.message == "success0"){
        $("#"+chargingstationid).empty();
        var img = "<img style='margin: 0px 0px 0px 11px;' src='./images/wx_map_collction.png' onclick='mpCollectionStationMassage(\""+chargingstationid+"\")'/>";
        $("#"+chargingstationid).append(img);
    }else {
        console.log("收藏失败请稍后收藏...");
    }


};